import { constantRoutes, asyncRoutes, resetRouter } from '@/router'
const state = {
  routes: constantRoutes
}
const mutations = {
  setRoutes(state, otherRoutes) {
    state.routes = [...constantRoutes, ...otherRoutes, { path: '*', redirect: '/404', hidden: true }]
  },
  clearRoutes(state) {
    // 清空vuex中的路由规则
    state.routes = constantRoutes
    // 清空router对象中的路由规则
    resetRouter()
  }
}
const actions = {
  async filterRoutes({ commit }, menus) {
    const otherRoutes = asyncRoutes.filter(item => {
      return menus.includes(item.children[0].name)
    })
    commit('setRoutes', otherRoutes)
    return otherRoutes
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
